package com.naizhuai.util;

import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.CheckResult;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.naizhuai.Model.db.user.PersonUserBean;
import com.naizhuai.R;

/**
 * dialog的工具类
 * Created by code_han on 2018/2/21.
 */

public class DialogUtils {

    //多项选择的dialog
    public static void showMultiDialog(Context mContext, String titleStr, String[] choiceItems, DialogInterface.OnClickListener chooseListener) {
        AlertDialog.Builder listDialog = new AlertDialog.Builder(mContext);
        if (titleStr != null)
            listDialog.setTitle(titleStr);
        listDialog.setItems(choiceItems, chooseListener);
        listDialog.show();
    }

    //单个按钮的提示弹框
    public static void showPromptDialog(Context mContext, String contentStr) {
        AlertDialog.Builder normalDialog = new AlertDialog.Builder(mContext);
        View view=View.inflate(mContext,R.layout.dialog_prompt_click,null);
        TextView content=view.findViewById(R.id.tv_content);
        TextView click=view.findViewById(R.id.tv_sure);
        content.setText(contentStr);
        normalDialog.setView(view);
//        normalDialog.setMessage(contentStr);
//        normalDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                dialog.dismiss();
//            }
//        });
        AlertDialog dialog=normalDialog.show();
        click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }


    //确定按钮弹框
    public static void showSureDialog(Context mContext, String contentStr,View.OnClickListener sureListener) {
        AlertDialog.Builder normalDialog = new AlertDialog.Builder(mContext);
        View view=View.inflate(mContext,R.layout.dialog_sure_click,null);
        TextView content=view.findViewById(R.id.tv_content);
        TextView sure=view.findViewById(R.id.tv_sure);
        TextView cancel=view.findViewById(R.id.tv_cancel);
        content.setText(contentStr);
        normalDialog.setView(view);
//        normalDialog.setMessage(contentStr);
//        normalDialog.setPositiveButton("确定", sureListener);
//        normalDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                dialog.dismiss();
//            }
//        });
        AlertDialog dialog=normalDialog.show();
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sureListener.onClick(v);
                dialog.dismiss();
            }
        });
    }

    public static void showCustomizeDialog(Context mContext, String titleStr, View dialogView, DialogListener sureClickListener) {
        AlertDialog.Builder customizeDialog = new AlertDialog.Builder(mContext);
        if(titleStr!=null)
            customizeDialog.setTitle(titleStr);
        customizeDialog.setCancelable(false);
        customizeDialog.setView(dialogView);
        customizeDialog.setPositiveButton("确定", null);
        customizeDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialogView.setTag(null);
                dialog.dismiss();
            }
        });
        AlertDialog dialog = customizeDialog.show();
        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(sureClickListener.onClick(v)){
                    dialog.dismiss();
                }
            }
        });
    }
    @CheckResult
    public static AlertDialog showNoBtnDialog(Context mContext, String titleStr, View dialogView) {
        AlertDialog.Builder customizeDialog = new AlertDialog.Builder(mContext);
        if(titleStr!=null)
            customizeDialog.setTitle(titleStr);
        customizeDialog.setView(dialogView);
        return customizeDialog.show();
    }

    public interface DialogListener{
        boolean onClick(View view);
    }

    public static boolean userFatigueSuitable(String fatigueValue,Context mContext){
        double value=MathUtils.canParseStringToDouble(fatigueValue);
        return value >= PersonUserBean.USER_MAX_FATIGUE_VALUE;
    }


}
